
<link rel="stylesheet" href="../addons/jy_supplier/css/mobile.css?v=1">
<div class='fui-page fui-page-current'>
    <div class="fui-header fui-header-success">
        <div class="fui-header-left">
            
        </div>
        <div class="title">商品编辑</div>
        <div class="fui-header-right"></div>
    </div>
    <div class='fui-content navbar'>
        <div class="fui-cell-group">
            <div class="fui-cell ">
                <div class="fui-cell-label ">商品名称</div>
                <div class="fui-cell-info">
                    <textarea rows="2" placeholder="请输入商品名称" ng-model="pagedata.item.title" id="title"></textarea>
                </div>
            </div>
            <div class="fui-cell">
                <div class="fui-cell-label">副标题</div>
                <div class="fui-cell-info">
                    <input type="text" placeholder="请输入商品副标题" class="fui-input" ng-model="pagedata.item.subtitle"   id="subtitle" />
                </div>
            </div>
			<div class="fui-cell">
				<label class="fui-cell-label">商品分类</label>
				<div class="fui-cell-info">
					<select ng-model="pagedata.item.pcate">
							<option value="" selected="selected">一级分类</option>
							<option ng-repeat="(value,data) in pagedata.pcates" value="{{data.id}}" ng-selected="pagedata.item.pcate==data.id">{{data.title}}</option>
					</select>
				</div>
				<div class="fui-cell-info">
							<select ng-model="pagedata.item.ccate">
							<option value="" selected="selected">二级分类</option>
							<option ng-repeat="(value,data) in pagedata.pcates[pagedata.item.pcate].cid" value="{{data.id}}" ng-selected="pagedata.item.ccate==data.id">{{data.title}}</option>
							</select>			
				</div>
			</div>
			<div class="fui-cell">
                <div class="fui-cell-label">商品首图</div>
                <div class="fui-cell-info">
                    <div class="fui-list jump">
								<a class="fui-list-media" ng-if="pagedata.item.image">
									<img class="round" src="{{tomedia(pagedata.item.image) || '../core/resource/images/nopic.jpg'}}" />
								</a>
								<a class="fui-list-media weui_uploader_input_wrp">
									<input class="weui_uploader_input" ng-click="image(pagedata.item,'image')" accept="image/jpg,image/jpeg,image/png,image/gif" multiple="">
								</a>
			</div>
                </div>
            </div>
			<div class="fui-cell">
                <div class="fui-cell-label">商品多图</div>
                <div class="fui-cell-info">
					<div class="weui_uploader">
                        <div class="weui_uploader_bd" style="margin-bottom:0">
                            <ul class="weui_uploader_files">
                                <li ng-repeat="p in pagedata.item.images" class="weui_uploader_file" style="background-image:url('{{tomedia(p)}}')">
									<div class="upbtn" >
										<a style="color:#f60" href="javascript:;" ng-click="pagedata.item.images.splice($index-1,0,p);pagedata.item.images.splice($index+1,1)">上移</a>
										<a style="color:#f60" href="javascript:;" ng-click="pagedata.item.images.splice($index,1)">删除</a>
									</div>
								</li>
                            </ul>
                            <div class="weui_uploader_input_wrp" href="javascript:;" ng-click="images(pagedata.item,'images')"></div>
                        </div>
                    </div>
				</div>
            </div>	
            <div class="fui-cell">
                <div class="fui-cell-label">单位</div>
                <div class="fui-cell-info">
                    <input type="text" placeholder="请输入商品单位" ng-model="pagedata.item.unit"  class="fui-input" id="unit" />
                </div>
            </div>
        </div>     
        <div class="fui-cell-group">
            <div class="fui-cell">
                <div class="fui-cell-label">原价</div>
                <div class="fui-cell-info">
                    <input  placeholder="请输入商品原价" class="fui-input"  ng-model="pagedata.item.originalprice"   id="productprice" />
                </div>
            </div>
            <div class="fui-cell">
                <div class="fui-cell-label">现价</div>
                <div class="fui-cell-info">
                    <input  placeholder="请输入现价" class="fui-input" ng-model="pagedata.item.price"  id="marketprice" />
                </div>
            </div>
            <div class="fui-cell">
                <div class="fui-cell-label">成本价</div>
                <div class="fui-cell-info">
                    <input  placeholder="请输入商品成本价" class="fui-input" ng-model="pagedata.item.costprice"  id="costprice" />
                </div>
            </div>
        </div>
        <div class="fui-cell-group fui-cell-click">
            <div class="fui-cell noactive">
                <div class="fui-cell-label">库存</div>
               
                    <div class="fui-cell-info">
                        <input type="tel" placeholder="请输入商品库存" class="fui-input"  ng-model="pagedata.item.total"  id="total" />
                    </div>
               
            </div>
            
            <div class="fui-cell noactive">
                <div class="fui-cell-label ">显示库存</div>
                <div class="fui-cell-info"><input type="checkbox" ng-checked="pagedata.item.isshowtotal==1" ng-model="pagedata.item.isshowtotal" class="fui-switch fui-switch-small fui-switch-success pull-right" id="showtotal" ></div>
            </div>
      
                <div class="fui-cell">
                    <div class="fui-cell-label">商品重量</div>
                    <div class="fui-cell-info">
                        <input  placeholder="请输入商品重量" class="fui-input" ng-model="pagedata.item.weight"  id="weight" />
                    </div>
                </div>
                <div class="fui-cell">
                    <div class="fui-cell-label">商品编码</div>
                    <div class="fui-cell-info">
                        <input  placeholder="请输入商品编码" class="fui-input" ng-model="pagedata.item.goodssn" id="goodssn" />
                    </div>
                </div>
                <div class="fui-cell">
                    <div class="fui-cell-label">商品条码</div>
                    <div class="fui-cell-info">
                        <input  placeholder="请输入商品条码" class="fui-input" ng-model="pagedata.item.productsn" id="productsn" />
                    </div>
                </div>
           
        </div>
       

        <div class="fui-title">浏览权限</div>
        <div class="fui-cell-group fui-cell-click">
            <div class="fui-cell check-param" data-action="viewlevel">
                <div class="fui-cell-label">会员等级</div>
                 <select class="weui_select" name="select2" ng-model="pagedata.showlevels">
                        <option ng-repeat="q in pagedata.levels" value="{{q.id}}">{{q.title}}</option>
                    </select>
                <div class="fui-cell-remark"></div>
            </div>
            <div class="fui-cell check-param" data-action="viewgroup">
                <div class="fui-cell-label">会员分组</div>
                <select class="weui_select" name="select2" ng-model="pagedata.showgroups">
                        <option ng-repeat="w in pagedata.groups" value="{{w.id}}">{{w.title}}</option>
                    </select>
                <div class="fui-cell-remark"></div>
            </div>
        </div>

        <div class="fui-title">购买权限</div>
        <div class="fui-cell-group fui-cell-click">
            <div class="fui-cell check-param" data-action="buylevel">
                <div class="fui-cell-label">会员等级</div>
               <select class="weui_select" name="select2" ng-model="pagedata.buylevel">
                        <option ng-repeat="e in pagedata.levels" value="{{e.id}}">{{e.title}}</option>
                    </select>
                <div class="fui-cell-remark"></div>
            </div>
            <div class="fui-cell check-param" data-action="buygroup">
                <div class="fui-cell-label">会员分组</div>
               <select class="weui_select" name="select2" ng-model="pagedata.buygroup">
                        <option ng-repeat="r in pagedata.groups" value="{{r.id}}">{{r.title}}</option>
                    </select>
                <div class="fui-cell-remark"></div>
            </div>
        </div>
        <div class="fui-title">商品限购(0为不限)</div>
        <div class="fui-cell-group fui-cell-click">
            <div class="fui-cell">
                <div class="fui-cell-label">单次最多购</div>
                <div class="fui-cell-info">
                    <input placeholder="单次最多购买数量" class="fui-input"  ng-model="pagedata.item.maxbuy" id="maxbuy" />
                </div>
                <div class="fui-cell-remark noremark">件</div>
            </div>
            <div class="fui-cell">
                <div class="fui-cell-label">单次最低购</div>
                <div class="fui-cell-info">
                    <input placeholder="单次最低购买数量" class="fui-input"  ng-model="pagedata.item.minbuy" id="minbuy" />
                </div>
                <div class="fui-cell-remark noremark">件</div>
            </div>
            <div class="fui-cell">
                <div class="fui-cell-label">会员总限购</div>
                <div class="fui-cell-info">
                    <input  placeholder="会员总限购买数量" class="fui-input"  ng-model="pagedata.item.usermaxbuy" id="usermaxbuy" />
                </div>
                <div class="fui-cell-remark noremark">件</div>
            </div>
        </div>

        
        <div class="fui-cell-group">
            <div class="fui-cell ">
                <div class="fui-cell-label ">货到付款</div>
                <div class="fui-cell-info"><input type="checkbox" ng-checked="pagedata.item.cashpay==1" ng-model="pagedata.item.cashpay" class="fui-switch fui-switch-small fui-switch-success pull-right" id="cash" ></div>
            </div>
            <div class="fui-cell ">
                <div class="fui-cell-label ">发票</div>
                <div class="fui-cell-info"><input type="checkbox" ng-checked="pagedata.item.invoice==1"  ng-model="pagedata.item.invoice" class="fui-switch fui-switch-small fui-switch-success pull-right" id="invoice" ></div>
            </div>
        </div>


        <div class="fui-cell-group fui-cell-click">
            <div class="fui-cell check-param" data-action="dispatch">
                <div class="fui-cell-label">运费设置</div>
                <input  placeholder="统一运费" class="fui-input"  ng-model="pagedata.item.dispatchprice" id="dispatchprice" />
                <div class="fui-cell-remark"></div>
            </div>
        </div>

        <div class="fui-cell-group fui-cell-click">
            <div class="fui-cell noactive">
                <div class="fui-cell-label ">商品上架</div>
                <div class="fui-cell-info"><input type="checkbox" ng-checked="pagedata.item.status==1" ng-model="pagedata.item.status" class="fui-switch fui-switch-small fui-switch-success pull-right" id="status" ></div>
            </div>
        </div>

        <div class="fui-cell-group">
            <div class="fui-cell">
                <div class="fui-cell-label">商品排序</div>
                <div class="fui-cell-info">
                    <input  placeholder="数字越大，排名越靠前" class="fui-input" ng-model="pagedata.item.displayorder" id="displayorder" />
                </div>
            </div>
        </div>
		<!--商品规格-->
		<style>
		.btn {line-height:1.4rem}
		.input-group .form-control {min-width:1.2rem}
		</style>
		<div ng-init="checkobj(pagedata.item,'specs');checkarray(pagedata.item.specs,'specs');"></div>
		<div class="weui_cells spec_item" ng-repeat="spec in pagedata.item.specs.specs">
				<div class="weui_cell">
					<div class="weui_cell_hd">规格名称</div>
					<div class="weui_cell_bd weui_cell_primary">
						<div class="input-group">
							<input class="weui_input spec_title" type="text" ng-model="spec.title" placeholder="规格名称 (比如: 颜色)">
						</div>
					</div>
					<div class="weui_cell_ft">
								<button class="btn btn-warning add-specitem" ng-click="new_option={};spec.options.push(new_option)" type="button" style="margin-right:0"><i class="fa fa-plus"></i>规格项</button>
								<button class="btn btn-info" ng-click="pagedata.item.specs.specs.splice($index,1)" type="button" style="margin-left:0"><i class="fa fa-remove"></i></button>
					</div>
				</div>
				<div class="weui_cell">
					<div class="weui_cell_hd">规格详情</div>
					<div class="weui_cell_bd weui_cell_primary">
						<div class="spec_item_items">
							<div ng-repeat="option in spec.options" class="spec_item_item" style="float:left;margin:5px;width:5rem; position: relative">
								<div class="input-group">
									<input type="text" class="weui_input spec_item_title" style="margin:0" ng-model="option.title"/>
									<span class="input-group-addon">
										<a href="javascript:;" ng-click="spec.options.splice($index,1)" title='删除' style="color:#fff"><i class="fa fa-times"></i></a>
									</span>
								</div>
							</div>
						</div>
					</div>
				</div> 
		</div>
		<div class="weui_cell">
			<div class="weui_cell_hd">
					<button class="btn btn-primary" ng-click="new_spec={'title':'','options':[]};pagedata.item.specs.specs.push(new_spec);" type="button"><i class="fa fa-plus"></i> 添加规格</button>
					<button class="btn btn-info" ng-click="pagedata.item.specs.tds=func.spec_made()" type="button"><i class="fa fa-refresh"></i> 刷新规格项目表</button>
			</div>
		</div>
		<div class="tips tips-yellow">
			<i class="fa fa-bullhorn"></i> [<red>温馨提示</red>]：设置好后,务必刷新一次规格,才会生效！
		</div>
		<div class="clearfix table-responsive" ng-if="pagedata.item.specs.tds">
			<table class="table table-bordered table-condensed jinyun-table">
				<thead class="navbar-inner">
					<tr class="active">
						<th width="80px" style="text-align:center;" ng-repeat="spec in pagedata.item.specs.specs">{{spec.title}}</th>
						<th width="80px" style="text-align:center;">图片</th>
						<th>
							<div class="">
							<div style="text-align:center;">库存</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.num">
							<span class="input-group-addon" ng-click="func.spec_set('num',pagedata.num)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
						<th>
							<div class="">
							<div style="text-align:center;">现价</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.price">
							<span class="input-group-addon" ng-click="func.spec_set('price',pagedata.price)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
						<th>
							<div class="">
							<div style="text-align:center;">原价</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.old_price">
							<span class="input-group-addon" ng-click="func.spec_set('old_price',pagedata.old_price)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
						<th>
							<div class="">
							<div style="text-align:center;">成本价</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.cost_price">
							<span class="input-group-addon" ng-click="func.spec_set('cost_price',pagedata.cost_price)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
						<th>
							<div class="">
							<div style="text-align:center;">编码</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.bm">
							<span class="input-group-addon" ng-click="func.spec_set('bm',pagedata.bm)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
						<th>
							<div class="">
							<div style="text-align:center;">条码</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.tm">
							<span class="input-group-addon" ng-click="func.spec_set('tm',pagedata.tm)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
						<th>
							<div class="">
							<div style="text-align:center;">重量</div>
							<div class="input-group">
							<input type="text" class="form-control  input-sm option_stock_all valid" ng-model="pagedata.weight">
							<span class="input-group-addon" ng-click="func.spec_set('weight',pagedata.weight)">
								<i class="fa fa-angle-double-down" title="批量设置"></i>
							</span>
							</div>
							</div>
						</th>
					</tr>
				</thead>
					<tr ng-repeat="(index,td) in pagedata.item.specs.tds">
						<td class="text-left full" style="text-align:center;"ng-repeat="(key,spec) in pagedata.item.specs.specs" ng-if="td[spec.title]!=pagedata.item.specs.tds[index-1][spec.title]" rowspan="{{pagedata.item.specs.specs[key+1].options.length || 1}}">
							{{td[spec.title]}}
						</td>
						<td style="text-align:center;" ng-click="image(td,'image')"><img ng-src="{{tomedia(td.image) || '../core/resource/images/nopic-small.jpg'}}" style="width:30px;height:30px;padding:1px;border:1px solid #ccc;"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.num"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.price"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.old_price"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.cost_price"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.bm"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.tm"/></td>
						<td><input class="form-control option_stock option valid" type="text" ng-model="td.weight"/></td>
					</tr>
			</table>
		</div>
		<script>
		window.scope.func.spec_set=function(key,value){
			$.each(window.scope.pagedata.item.specs.tds,function(index,td){
				td[key]=value;
			});
		}
		window.scope.func.spec_made=function(){
			//刷新规格项目表，如果只是增加规格项，则保留原有规格商品，如何保留呢。两个问题：如何判断是仅改规格项？两个规格数据库都存有。如何保留原有规格商品？生成tds后，遍历原tds，然后进行赋值
			var isnew=false;
			$.each(scope.pagedata.item.specs.specs,function(i,spec){
				if(!spec.id){
					isnew=true;
				}
			});
			if(!scope.pagedata.item.specs.tds || scope.pagedata.item.specs.tds.length<=0){
				isnew=true;
			}
			
			var tds=[{'num':0}];
			$.each(window.scope.pagedata.item.specs.specs,function(i,spec){
				var new_tds=[];
				$.each(tds,function(j,k){
					$.each(spec.options,function(l,m){
						var new_td=angular.copy(k);
						new_td[spec.title]=m.title;
						new_tds.push(new_td);
					});
					
				});
				tds=new_tds;
			});
			if(!isnew){
				$.each(tds,function(j,k){
					$.each(scope.pagedata.item.specs.tds,function(j_old,k_old){
						var like=true;
						$.each(scope.pagedata.item.specs.specs,function(i,spec){
							if(k[spec.title]!=k_old[spec.title]){
								like=false;
							}
						});
						if(like){
							tds[j]=angular.copy(k_old);;
						}
					});
				})
			}
			return tds;
		}
		</script>
        <div class="fui-title center">更多设置请至PC端后台</div>

    </div>



    <div class="fui-navbar">
        <div ng-click="post(pagedata.item,'post')" class="nav-item btn btn-success btn-submit">保存</div>
    </div>


</div>
